class Libsecret < Formula
  desc "Library for storing/retrieving passwords and other secrets"
  homepage "https://wiki.gnome.org/Projects/Libsecret"
  url "https://download.gnome.org/sources/libsecret/0.21/libsecret-0.21.6.tar.xz"
  sha256 "747b8c175be108c880d3adfb9c3537ea66e520e4ad2dccf5dce58003aeeca090"
  license all_of: ["LGPL-2.1-or-later", "GPL-2.0-or-later", "Apache-2.0"]

  # libsecret doesn't use GNOME's "even-numbered minor is stable" version
  # scheme. This regex is the same as the one generated by the `Gnome` strategy
  # but it's necessary to avoid the related version scheme logic.
  livecheck do
    url :stable
    regex(/libsecret[._-]v?(\d+(?:\.\d+)+)\.t/i)
  end

  bottle do
    sha256 cellar: :any, arm64_sequoia: "4276a39237897a4ab5d19eadb4ff5039f4e6d277ae72b5e2d1e86e50f32f7f2d"
    sha256 cellar: :any, arm64_sonoma:  "295d7e264d8ee325eb0ff2143f02ad81148495e47cd1dd4e7174661f4dfe5dc2"
    sha256 cellar: :any, arm64_ventura: "628038371a126d7c0ce143990a42d758bc1bdf2b59524a42763a689cb4477496"
    sha256 cellar: :any, sonoma:        "84dd23695183b1b623265e938722950ab9b6a808a1d36d1654fd7ee002fea297"
    sha256 cellar: :any, ventura:       "4f156a011bda9e27985a1cf8ccc7ff88fd4c5b5742ab01dcf7d1d3d69bfcacf3"
    sha256               x86_64_linux:  "1d3495802ba6577f91b1c62e5270b0af6950cb0c7f7f5d08bcf5c24f2b09ef91"
  end

  depends_on "docbook-xsl" => :build
  depends_on "gettext" => :build
  depends_on "gobject-introspection" => :build
  depends_on "meson" => :build
  depends_on "ninja" => :build
  depends_on "pkgconf" => [:build, :test]
  depends_on "vala" => :build
  depends_on "glib"
  depends_on "libgcrypt"
  uses_from_macos "libxslt" => :build

  on_macos do
    depends_on "gettext"
  end

  def install
    ENV["XML_CATALOG_FILES"] = "#{etc}/xml/catalog"

    system "meson", "setup", "build", "-Dbashcompdir=#{bash_completion}",
                                      "-Dgtk_doc=false",
                                      *std_meson_args
    system "meson", "compile", "-C", "build", "--verbose"
    system "meson", "install", "-C", "build"
  end

  test do
    (testpath/"test.c").write <<~C
      #include <libsecret/secret.h>

      const SecretSchema * example_get_schema (void) G_GNUC_CONST;

      const SecretSchema *
      example_get_schema (void)
      {
          static const SecretSchema the_schema = {
              "org.example.Password", SECRET_SCHEMA_NONE,
              {
                  {  "number", SECRET_SCHEMA_ATTRIBUTE_INTEGER },
                  {  "string", SECRET_SCHEMA_ATTRIBUTE_STRING },
                  {  "even", SECRET_SCHEMA_ATTRIBUTE_BOOLEAN },
                  {  "NULL", 0 },
              }
          };
          return &the_schema;
      }

      int main()
      {
          example_get_schema();
          return 0;
      }
    C

    flags = shell_output("pkgconf --cflags --libs libsecret-1").chomp.split
    system ENV.cc, "test.c", "-o", "test", *flags
    system "./test"
  end
end
